﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using blackhouse.Aparts.Web.Model;
using blackhouse.Aparts.Web.View;
using blackhouse.BlackApart.Business.Opinions;

namespace blackhouse.Aparts.Web.Presenter {
    public class OpinionsPresenter {

        #region Fields

        private readonly IOpinionsView view;
        private readonly OpinionsModel model;
        public const int OPINION_PART_COUNT = 10;

        #endregion

        #region Constructors

        public OpinionsPresenter(IOpinionsView view, OpinionsModel model) {
            this.view = view;
            this.model = model;
        }

        #endregion

        #region Methods

        public void LoadSummaryReviews() {

            int basedOnOpinions = this.model.GetBasedOnOpinions(this.view.ApartmentTypeId);
            this.view.SatisfactedClients = this.model.GetStatisfactedClientsCount();

            if (basedOnOpinions > 0) {
                double avg = this.model.GetAverageRating(this.view.ApartmentTypeId);
                this.view.AverageReviews = avg;
                this.view.OpinionsCount = basedOnOpinions;
                this.SetSummaryCategories();
            }
            else {
                this.view.AverageReviews = -1;
                this.view.OpinionsCount = 0;
                this.SetEmptySummaryCategories();
            }

        }

        public void LoadOpinions() {
            this.LoadOpinions(this.view.ApartmentTypeId);
        }

        private void LoadOpinions(int filterByApartment) {
            var opinions = (from o in this.model.GetOpinions(this.view.HaveOpinionAdminPriv) where filterByApartment == 0 || o.GetAssociatedApartmentGroupId() == filterByApartment orderby o.PublishedDate descending select o).Take(OPINION_PART_COUNT).ToList();
            if (opinions.Count > 0)
                this.view.DisplayOpinions = opinions;
            else
                this.view.ShowNoOpinionsInfo();
        }

        public void LoadAllOpinions() {
            var opinions = (from o in this.model.GetOpinions(this.view.HaveOpinionAdminPriv) orderby o.PublishedDate descending select o);
            this.view.DisplayOpinions = opinions.ToList();
        }

        private void SetSummaryCategories() {
            int filterByApartment = this.view.ApartmentTypeId;
            IOpinionCategory[] categories = this.model.GetOpinionCategories();
            Dictionary<string, double> list = new Dictionary<string, double>();
            foreach (IOpinionCategory category in categories) {
                string catName = category.Name;
                if (!string.IsNullOrEmpty(category.NameContinued))
                    catName = catName + " (" + category.NameContinued + ")";
                list.Add(catName, this.model.GetSummaryCategoryValue(filterByApartment, category.CategoryId));
            }
            var q = from entry in list orderby entry.Value descending select entry;
            this.view.ReviewCategoriesValue = q.ToDictionary(pair => pair.Key, pair => pair.Value);
        }

        private void SetEmptySummaryCategories() {
            IOpinionCategory[] categories = this.model.GetOpinionCategories();
            Dictionary<string, double> list = new Dictionary<string, double>();
            foreach (IOpinionCategory category in categories) {
                string catName = category.Name;
                if (!string.IsNullOrEmpty(category.NameContinued))
                    catName = catName + " (" + category.NameContinued + ")";
                list.Add(catName, 0);
            }
            var q = from entry in list orderby entry.Value descending select entry;
            this.view.ReviewCategoriesValue = q.ToDictionary(pair => pair.Key, pair => pair.Value);
        }

        #endregion

    }
}